package com.woniu.commons.exception;

import com.woniu.zsd.commons.core.model.Result;
import com.woniu.zsd.commons.exception.ZsdException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ValidationException;

@RestControllerAdvice
public class MovieExceptionHandler {
    private static final Logger log = LoggerFactory.getLogger(MovieExceptionHandler.class);

    public MovieExceptionHandler() {
        log.info("MovieExceptionHandler initialized");
    }

    //类中的每一个方法用于处理一种不同的异常
    @ExceptionHandler(ZsdException.class)
    public Result handleManagerException(ZsdException e){
        return Result.getResult().setCode(e.getCode()).setMsg(e.getMessage());
    }

    @ExceptionHandler(ValidationException.class)
    public Result validationException(ValidationException e) {
        // 获取所有字段错误信息
        System.out.println(123);

        // 返回自定义结果
        return Result.getResult()
                .setCode(400) // 设置状态码为 400 表示客户端错误
                .setMsg("参数有问题请重新校验");
    }

    /**
     * 兜底操作，处理系统异常
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e) {
        e.printStackTrace();
        return Result.getResult().setCode(500).setMsg("系统错误");
    }
}
